#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>

#include "core.h"
#include "guess_number.h"
#include "file_operation.h"
#include "user_operation.h"

/*
 *@brief        初始化用于全局的结构体
 *@return       返回指向创建节点的指针
 */
struct core *init(void)
{
    struct core* core_data = NULL;
	
    core_data = (struct core*)malloc(sizeof(struct core));
    
    core_data->head.grace = 0;
    strcpy(core_data->head.name, "1"); 
    strcpy(core_data->head.code, "1"); 
	
	core_data->head.next = NULL;
	read_from_file(&core_data->head);
	
    return core_data;
}

void init_guess_game(struct guess* core_guess_data)
{
	if (!core_guess_data) {
        DEBUG("PARAM ERROR!\n");
        return;
    }
    
    core_guess_data->biggest_number = 100;
    core_guess_data->small_number = 0;
    
    core_guess_data->right_number = get_random();
}

void run(struct core* core)
{
    if (!core) {
        DEBUG("PARAM ERROR!\n");
        return;
    }
    
    menu(core);
}

/*
 *@brief        检测猜数字输入的字符是否为纯数字
 *@param        用户猜测的数字
 *@return       用户输入的纯数字
 */
int judge_number(int first_input_number)
{
	int count;
	int ret = 1;
	int length = 0;
	char change_input_number[10];
		    
	while (1) {
		scanf("%s",change_input_number);
		       
		length = strlen(change_input_number);
			   
		for (count = 0; count < length; count++) {
			if (change_input_number[count] >= '0' && change_input_number[count] <= '9') {
				ret = 1;
				continue;
			} 
			else {
				printf("\n\t\tplease just input number:");
				ret = 0;
				break;
			}
		}
		
		if (ret == 0) {
			continue;
		}
		
		first_input_number = atoi(change_input_number);
		break;
	}
	
	return first_input_number;
}
